﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StandardManager.EntityCode
{
    public partial class EntityCodeItem : Form
    {
        public EntityCode ParentItem { get; set; }
        public EntityCode SelectedItem { get; set; }
        public int CurrentSelectItemLevel { get; set; }
        public event EventHandler OnPatchItemAdd;

        private bool m_isUpdate;
        public EntityCodeItem()
        {
            InitializeComponent();
        }
        private void EntityCodeItem_Load(object sender, EventArgs e)
        {
            if (SelectedItem != null)
            {
                m_isUpdate = true;
                this.textBoxItems.Enabled = false;
                this.textBox1.Text = SelectedItem.CodeValue;
                this.textBox2.Text = SelectedItem.Name;
                this.textBox3.Text = SelectedItem.Description;
                this.labelLevels.Text = (CurrentSelectItemLevel + 1).ToString();
            }
            if (ParentItem != null)
            {
                this.labelLevel.Text = ParentItem.Name;
                this.labelParentCode.Text = ParentItem.CodeValue;
            }

            this.textBox1.KeyDown += ((object? sender, KeyEventArgs e) =>
                {
                    if (e.KeyCode == Keys.Escape)
                    {
                        this.Close();
                    }
                });
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button1_Click(null, null);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (m_isUpdate == false)
            {
                if (string.IsNullOrEmpty(this.textBoxItems.Text) == false)
                {
                    if (listView1.Items.Count == 0)
                    {
                        MessageBox.Show("你需要先Try一下，避免错误");
                        return;
                    }
                    if (OnPatchItemAdd != null)
                    {
                        for (var i = 0; i < listView1.Items.Count; i++)
                        {
                            var tr = listView1.Items[i];
                            SelectedItem = new EntityCode()
                            {
                                Id = Guid.NewGuid().ToString(),
                                CodeValue = tr.Text,
                                Name = tr.SubItems[1].Text,
                            };
                            if (ParentItem != null)
                            {
                                SelectedItem.ParentId = ParentItem.Id;
                            }
                            OnPatchItemAdd(SelectedItem, null);
                        }
                        SelectedItem = null;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                        return;
                    }
                }
            }
            if (m_isUpdate == false)
            {
                SelectedItem = new EntityCode()
                {
                    Id = Guid.NewGuid().ToString(),
                    CodeValue = this.textBox1.Text,
                    Name = this.textBox2.Text,
                    Description = this.textBox3.Text
                };
            }
            else
            {
                SelectedItem.CodeValue = this.textBox1.Text;
                SelectedItem.Name = this.textBox2.Text;
                SelectedItem.Description = this.textBox3.Text;
            }
            if (ParentItem != null)
            {
                SelectedItem.ParentId = ParentItem.Id;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var sp = this.textBoxSpliter.Text.ToCharArray();
            var sp2 = new List<char>();
            sp2.AddRange(sp);
            sp2.Add('\r');
            var ds = this.textBoxItems.Text.Trim().Split(sp2.ToArray());
            this.listView1.Items.Clear();

            if (this.checkBox1.Checked)
            {
                var dd = (int)this.numericUpDown1.Value;
                var row = (int)this.numericUpDownFrom.Value;
                for (var i = 0; i < ds.Length; i++)
                {
                    if (string.IsNullOrEmpty(ds[i].Trim())) { continue; }
                    var code = (row).ToString().PadLeft(dd, '0');
                    var lvi = new ListViewItem(code);
                    lvi.SubItems.Add(ds[i].Trim());
                    this.listView1.Items.Add(lvi);
                    row++;
                }
            }
            else
            {
                for (var i = 0; i < ds.Length; i += 2)
                {
                    var lvi = new ListViewItem(ds[i].Trim());
                    lvi.SubItems.Add(ds[i + 1].Trim());
                    this.listView1.Items.Add(lvi);
                }

            }
        }

        
    }
}
